package com.tjjingpan.wifidirectdemo.adapter

import android.net.wifi.p2p.WifiP2pDevice
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.tjjingpan.wifidirectdemo.R
import com.tjjingpan.wifidirectdemo.model.WiFiDirectDevice
import com.tjjingpan.wifidirectdemo.utils.WifiP2pUtils

class WifiDirectDeviceAdapter(private val wifiDirectDeviceList: ArrayList<WiFiDirectDevice>) :
    RecyclerView.Adapter<WifiDirectDeviceAdapter.ViewHolder>() {
    val TAG = "WifiDirectDeviceAdapter"

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        val deviceName: TextView = view.findViewById(R.id.tv_deviceName)
        val deviceAddress: TextView = view.findViewById(R.id.tv_deviceAddress)
        val deviceDetails: TextView =view.findViewById(R.id.tv_deviceDetails)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_device, parent, false)
        Log.d(TAG,"onCreateViewHolder")
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        Log.d(TAG,"onBindViewHolder")
        holder.deviceName.text = wifiDirectDeviceList[position].deviceName
        holder.deviceAddress.text = wifiDirectDeviceList[position].deviceAddress
        holder.deviceDetails.text = WifiP2pUtils.getDeviceStatus(wifiDirectDeviceList[position].status)
    }
    override fun getItemCount() = wifiDirectDeviceList.size
}